package com.qingzhuge.manager.controller;


import com.qingzhuge.common.Constants;
import com.qingzhuge.common.HttpCode;
import com.qingzhuge.common.Resources;
import com.qingzhuge.common.annotation.ASysLog;
import com.qingzhuge.common.annotation.ASysLogMethod;
import com.qingzhuge.common.enums.EResourceType;
import com.qingzhuge.core.annotation.AResource;
import com.qingzhuge.dto.response.ResponseBodyDto;
import com.qingzhuge.exception.ValidatorException;
import com.qingzhuge.manager.controller.base.BaseUidPkLongController;
import com.qingzhuge.manager.dto.request.SysUserRequestDto;
import com.qingzhuge.manager.dto.request.SysUserResetPasswordRequestDto;
import com.qingzhuge.manager.dto.response.SysUserResponseDto;
import com.qingzhuge.manager.entity.SysUser;
import com.qingzhuge.manager.security.util.SecurityUtils;
import com.qingzhuge.manager.service.ISysUserService;
import com.qingzhuge.resource.entity.SysResource;
import com.qingzhuge.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.qingzhuge.common.Constants.Controller.PATH_INFO;

/**
 * @author : zero.xiao
 * @description : 用户管理
 * @date :2018/6/8 0008 下午 14:51
 * @modified :
 */
@ASysLog("用户管理")
@Api(tags = "S用户管理")
@AResource(name = "用户管理", type = EResourceType.menu, catalog = "系统管理")
@RestController
@RequestMapping("api/sys/auth/user")
public class SysUserController extends BaseUidPkLongController<
        SysUser, ISysUserService, SysUserRequestDto, SysUserResponseDto> {

    @ApiOperation(value = "系统用户数量")
    @GetMapping("/count")
    public ResponseEntity<ResponseBodyDto<Integer>> count(){
        return returnSuccess(service.countByRootFalse());
    }

    @Override
    public ResponseEntity<ResponseBodyDto<PageUtil<SysUserResponseDto>>> queryPage(@RequestBody(required = false) SysUserRequestDto param) {
        return super.queryPage(param);
    }

    @ApiOperation(value = "当前用户")
    @GetMapping(PATH_INFO)
    public ResponseEntity<ResponseBodyDto<Map>> info() {
        Long uid = SecurityUtils.getUserId();
        if (null == uid) {
            return returnFailed(HttpCode.FORBIDDEN.message());
        }
        Map<String,Object> modelMap = new HashMap<>(16);
        SysUser sysUser = service.findById(uid);
        modelMap.put("user", sysUser);
        Collection<SysResource> resources = service.queryResourceByUid(uid);
        modelMap.put("menus", resources);
        return returnSuccess(modelMap);
    }

    @ASysLogMethod(value = "修改密码")
    @ApiOperation(value = "修改密码")
    @PutMapping("/password")
    @PreAuthorize("authentication.principal.id == params.uid")
    public ResponseEntity<ResponseBodyDto<Object>> password(@RequestBody SysUserResetPasswordRequestDto params) {
        String password = params.getOldPassword();
        String newPassword = params.getNewPassword();
        if (StringUtils.isBlank(newPassword)) {
            throw new ValidatorException(Resources.getMessage(Constants.I18n.LOGIN.PASSWORD_IS_NULL));
        }
        if (StringUtils.isBlank(password)) {
            throw new ValidatorException(Resources.getMessage(Constants.I18n.LOGIN.OLDPASSWORD_IS_NULL));
        }
        //更新密码
        service.updatePassword(getUserId(), password, newPassword);
        return returnSuccess();
    }

    @Override
    public ResponseEntity<ResponseBodyDto<SysUserResponseDto>> get(@PathVariable Long id) {
        return super.get(id);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<SysUserResponseDto>> save(@RequestBody SysUserRequestDto user) {
        user.setRoot(false);
        return super.save(user);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<SysUserResponseDto>> update(@RequestBody SysUserRequestDto user) {
        user.setRoot(false);
        return super.update(user);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<Object>> delete(@PathVariable @NotBlank Long id) {
        return super.delete(id);
    }

    @ApiOperation(value = "按部门查找用户")
    @GetMapping("by/{id}")
    public ResponseEntity<ResponseBodyDto<List<SysUser>>> doUserByDept(@PathVariable @NotBlank Long id){
        return returnSuccess(service.findListByDept(id));
    }
}
